package tw.com.diray;

import tw.com.diary.db.domain.X0001;
import tw.com.diary.db.util.DBHelp;
import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Build;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ShareActionProvider;
import android.widget.TextView;
import android.widget.Toast;

/**
 * 觀看內容
 * 
 * @author Andy
 * 
 */
public class DariyView extends Activity {

	private final static String TEXT = "%s\n%s";

	private TextView textview;

	private ShareActionProvider mShareActionProvider;

	@TargetApi(Build.VERSION_CODES.HONEYCOMB)
	@SuppressLint("NewApi")
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_dariy_view);
		getActionBar().setDisplayHomeAsUpEnabled(true);

		final Bundle bundle = getIntent().getExtras();
		final String uuid = bundle.getString("uuid");

		DBHelp dbhelp = DBHelp.intance(this);

		SQLiteDatabase con = dbhelp.getReadableDatabase();
		Cursor cusor = con.rawQuery("select * from x0001 where uuid=? ",
				new String[] { uuid });
		cusor.moveToFirst();

		final X0001 x001 = new X0001();

		x001.setContent(cusor.getString(cusor.getColumnIndex("content")));
		x001.setUuid(cusor.getString(cusor.getColumnIndex("uuid")));
		x001.setYyyymmdd(cusor.getString(cusor.getColumnIndex("yyyymmdd")));

		cusor.close();

		this.textview = (TextView) this.findViewById(R.id.content);
		this.textview.setText(String.format(TEXT, x001.getYyyymmdd(),
				x001.getContent()));

	}

	@TargetApi(Build.VERSION_CODES.ICE_CREAM_SANDWICH)
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.dariy_view, menu);

		MenuItem item = menu.findItem(R.id.action_share);
		this.mShareActionProvider = (ShareActionProvider) item
				.getActionProvider();

		Intent shareIntent = new Intent();
		shareIntent.setAction(Intent.ACTION_SEND);
		shareIntent.putExtra(Intent.EXTRA_TEXT, this.textview.getText());
		shareIntent.addCategory(Intent.CATEGORY_DEFAULT);
		shareIntent.setType("text/plain");
		this.mShareActionProvider.setShareIntent(shareIntent);

		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		final int id = item.getItemId();
		switch (id) {
		case R.id.action_del:

			final Builder builder = new AlertDialog.Builder(this);

			builder.setMessage("確定刪除");
			builder.setPositiveButton("取消", null);
			builder.setNegativeButton("確定",
					new DialogInterface.OnClickListener() {

						@Override
						public void onClick(DialogInterface dialog, int which) {

							DBHelp dbhelp = DBHelp.intance(getApplicationContext());

							final Bundle bundle = getIntent().getExtras();
							final String uuid = bundle.getString("uuid");

							SQLiteDatabase con = dbhelp.getWritableDatabase();
							con.delete("x0001", "uuid=? ",
									new String[] { uuid });
							con.close();

							Toast.makeText(DariyView.this, "刪除完成",
									Toast.LENGTH_SHORT).show();

							DariyView.this.finish();

						}
					});
			builder.create().show();

			break;

		}

		// TODO Auto-generated method stub
		return super.onOptionsItemSelected(item);
	}

}
